home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Frameworks / Grant's CGI Framework 1.0b14 / Util / ErrorUtil.c < prev    next >
Text File  |  1996-04-10  |  6KB  |  226 lines

  1. /*****
  2.  *
  3.  *    ErrorUtil.c
  4.  *
  5.  *    This is a support file for "Grant's CGI Framework".
  6.  *    Please see the license agreement that accompanies the distribution package
  7.  *    for licensing details.
  8.  *
  9.  *    Copyright ©1995,1996 by Grant Neufeld
  10.  *    grant@acm.com
  11.  *    http://arpp.carleton.ca/grant/
  12.  *
  13.  *****/
  14.  
  15. #include "MyConfiguration.h"
  16.  
  17. #include <stdio.h>
  18. #include <string.h>
  19.  
  20. #include "compiler_stuff.h"
  21. #include "constants.h"
  22. #include "globals.h"
  23.  
  24. #include "DebugUtil.h"
  25. #include "ErrorInt.h"
  26. #include "LogUtil.h"
  27. #include "MemoryUtil.h"
  28. #include "Quit.h"
  29. #include "StringUtil.h"
  30.  
  31. #include "ErrorUtil.h"
  32.  
  33.  
  34. /***  LOCAL CONSTANTS ***/
  35.  
  36. #define kstrErrorMissing        "\pUnknown! Note the id number and contact the developer of this software."
  37.  
  38. /* the size of the above string + 1 for size byte */
  39. #define kstrErrorMissingSize    72
  40.  
  41.  
  42. /***  LOCAL PROTOTYPES ***/
  43.  
  44. static    StringHandle    errorSystemGetString        ( OSErr, Str255 * );
  45. static    void            errorSystemDisposeString    ( StringHandle );
  46. static    void            errorStartupGetString        ( short, Str255 * );
  47.  
  48.  
  49. /***  FUNCTIONS  ***/
  50.  
  51. /*  */
  52. p_export
  53. void
  54. ErrorSystemHandler ( OSErr errNum, Boolean useAlert )
  55. {
  56.     #if kCompileWithErrorLogging || (kCompileWithForeground && kCompileWithFullUserInterface)
  57.     StringHandle    errStrHdl;    /* the human readable error string */
  58.     Str255            errNameStr;    /* the name of the error */
  59.     #endif
  60.     #if kCompileWithErrorLogging
  61.     Str255            errNumStr;    /* used for string version of the error number */
  62.     #endif
  63.     
  64.     /* Create a pascal format string of the error number. */
  65.     #if kCompileWithErrorLogging
  66.     NumToString ( errNum, errNumStr );
  67.     LogStringAndSeparatorP ( errNumStr, '\t' );
  68.     #endif /* kCompileWithErrorLogging */
  69.     
  70.     #if kCompileWithErrorLogging || (kCompileWithForeground && kCompileWithFullUserInterface)
  71.     errStrHdl = errorSystemGetString ( errNum, &errNameStr );
  72.     if ( (errStrHdl != NULL) && (*errStrHdl != NULL) )
  73.     {
  74.         /* errStrHdl was successfully retreived */
  75.         
  76.         #if kCompileWithForeground && kCompileWithFullUserInterface
  77.         if ( useAlert )
  78.         {
  79.             /* Display the alert. */
  80.             ErrorAlertSystemString ( errNum, errStrHdl, &errNameStr );
  81.         }
  82.         #endif
  83.         
  84.         #if kCompileWithErrorLogging
  85.         LogStringAndSeparatorP ( errNameStr, '\t' );
  86.         HLock ( (Handle)errStrHdl );
  87.         LogStringBreakP ( *errStrHdl );
  88.         HUnlock ( (Handle)errStrHdl );
  89.         #endif
  90.         
  91.         errorSystemDisposeString ( errStrHdl );
  92.     }
  93.     else
  94.     {
  95.         /* can't get system error string */
  96.         #if kCompileWithErrorLogging
  97.         LogStringBreakP ( "\p(can't load error message string)" );
  98.         #endif /* kCompileWithErrorLogging */
  99.     #endif
  100.     
  101.         SysBeep ( 5 );
  102.     
  103.     #if kCompileWithErrorLogging || (kCompileWithForeground && kCompileWithFullUserInterface)
  104.     }
  105.     #endif
  106. } /* ErrorSystem */
  107.  
  108.  
  109. #pragma segment Startup
  110. /* A 'fatal' startup error has occurred. This usually means that a required
  111.     system software component is missing (IE. Apple Events) and/or the
  112.     System version is too old. */
  113. p_export
  114. void
  115. ErrorStartup ( short errNum )
  116. {
  117.     #if kCompileWithErrorLogging || (kCompileWithForeground && kCompileWithFullUserInterface)
  118.  
  119.     Str255    errStr;    /* the retrieved error string */
  120.     #if kCompileWithErrorLogging
  121.     Str255    errNumStr;    /* used for string version of the error number */
  122.     
  123.     /* Create a pascal format string of the error number. */
  124.     NumToString ( errNum, errNumStr );
  125.     LogStringAndSeparatorP ( errNumStr, '\t' );
  126.     #endif /* kCompileWithErrorLogging */
  127.     
  128.     errorStartupGetString ( errNum, &errStr );
  129.     
  130.     #if kCompileWithForeground && kCompileWithFullUserInterface
  131.     /* Display the alert. */
  132.     ErrorAlertStartupString ( (OSErr)errNum, &errStr );
  133.     #endif
  134.     
  135.     #if kCompileWithErrorLogging
  136.     LogStringBreakP ( errStr );
  137.     LogQuit ();
  138.     #endif /* kCompileWithErrorLogging */
  139.     
  140.     #endif /* kCompileWithErrorLogging || (kCompileWithForeground && kCompileWithFullUserInterface) */
  141.     
  142.     ExitToShell ();
  143. } /* ErrorStartup */
  144. #pragma segment Main
  145.  
  146.  
  147. /**  String Retreival  **/
  148.  
  149. /* return the system error's human readable string and it's constant name (errNameStr) */
  150. static StringHandle
  151. errorSystemGetString ( OSErr errNum, Str255 *errNameStr )
  152. {
  153.     StringHandle    errStrHdl;    /* the retrieved error string */
  154.     StringHandle    theString;    /* the string to return */
  155.     short            errID;        /* just used for GetResInfo call */
  156.     ResType            errType;    /* just used for GetResInfo call */
  157.     
  158.     /* Get message string for the error number. */
  159.     errStrHdl = (StringHandle) GetResource ( (ResType)krtErrStr, (short)errNum );
  160.     theString = NULL;
  161.     if ( errStrHdl != NULL )
  162.     {
  163.         theString = (StringHandle) MemoryNewHandle ( sizeof(Str255), NULL );
  164.     }
  165.     
  166.     if ( theString == NULL )
  167.     {
  168.         /* if string isn't available, use generic error string */
  169.         theString            = gSystemErrorStr;
  170.         (*errNameStr)[0]    = nil;
  171.     }
  172.     else
  173.     {
  174.         my_assert ( (HGetState((Handle)errStrHdl) & kMemoryHandleLockedFlag) == nil,
  175.             "\perrorSystemGetString: errStrHdl is already locked!" );
  176.         my_assert ( (HGetState((Handle)theString) & kMemoryHandleLockedFlag) == nil,
  177.             "\perrorSystemGetString: theString is already locked!" );
  178.         HLock ( (Handle)errStrHdl );
  179.         HLock ( (Handle)theString );
  180.         
  181.         StringPascalCopy ( (char *)(*errStrHdl), (char *)(*theString) );
  182.         
  183.         HUnlock ( (Handle)errStrHdl );
  184.         HUnlock ( (Handle)theString );
  185.         
  186.         ReleaseResource ( (Handle)errStrHdl );
  187.         
  188.         /* get the error name from the resource */
  189.         GetResInfo ( (Handle)errStrHdl, &errID, &errType, *errNameStr );
  190.     }
  191.     
  192.     return theString;
  193. } /* errorSystemGetString */
  194.  
  195.  
  196. /* dispose of the error string handle - don't do anything if it is the default string */
  197. static void
  198. errorSystemDisposeString ( StringHandle theString )
  199. {
  200.     my_assert ( theString != NULL, "\perrorSystemDisposeString: theString is NULL" );
  201.     
  202.     if ( theString != gSystemErrorStr )
  203.     {
  204.         DisposeHandle ( (Handle)theString );
  205.     }
  206. } /* errorSystemDisposeString */
  207.  
  208.  
  209. #pragma segment Startup
  210. /*  */
  211. static void
  212. errorStartupGetString ( short errNum, Str255 *errStr )
  213. {
  214.     /* Get message string for the error number. */
  215.     GetIndString ( *errStr, krStartupErrorStrs, errNum );
  216.     if ( *errStr == nil )
  217.     {
  218.         /* if string isn't available, use generic error string */
  219.         StringPascalCopy ( (char *)kstrErrorMissing, (char *)(*errStr) );
  220.     }
  221. } /* errorStartupGetString */
  222. #pragma segment Main
  223.  
  224.  
  225. /*****  EOF  *****/
  226.